<template>
	<view class="wui-form">
		<slot></slot>
	</view>
</template>

<script>
	export default {
		name: 'w-form',
		props: {
			// 验证规则
			rules: {
				type: Object,
				default: () => {}
			},
			// 数据对象
			model: {
				type: Object,
				default: () => {}
			},
			labelWidth: {
				type: [String, Number],
				default: '120'
			},
			labelPosition: {
				type: String,
				default: 'left'
			},
		},
		created() {
			this.componentsValidateResult = []
		},
		methods: {
			validate(callback) {
				let validArr = []
				let validFalse = undefined
				let validResult = {};
				this.componentsValidateResult.map(val => {
					val.validate(res => {
						validArr.push(res)
					})
				})
				validFalse = validArr.find((val) => val.valid === false)
				if (validFalse) {
					validResult = validFalse
				} else {
					validResult = {
						valid: true,
						message: ''
					}
				}
				return callback(validResult)
			},
		}
	};
</script>

<style lang="scss" scoped></style>
